/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Dimensions,
    Image,
    TouchableOpacity,
    TouchableHighlight,
    ScrollView,
} from 'react-native';
import Swiper from 'react-native-swiper';
import {request,apiStore,configure} from '../service/api'

const {width,height} =Dimensions.get('window');

const imageWidth=width;
const imageHeight =imageWidth/4*3;



export default class Swipers extends Component {

    constructor(props) {
        super(props);

        this.state = {
            images:null
        }
    }
    // fetchImg = () => {
    //     //let banner=this.state.banner;
    //     fetch('http://121.40.178.164:4040/react-service/api/systemConf/getByParamKey.do', {
    //         method: 'POST',
    //         headers: {
    //             'Content-Type': 'application/x-www-form-urlencoded',
    //         },
    //         body: 'paramKey'+'='+banner
    //     })
    //         .then((response) => response.json()) //把response转为json
    //         .then((responseData) => { // 上面的转好的json
    //             // let res=JSON.stringify(responseData);
    //
    //             if(responseData.code==0){
    //                 const result=responseData.res.data.paramValue.split(',');
    //                 alert(result)
    //
    //             }
    //
    //
    //             // this.setState({
    //             //     data:result,
    //             // })
    //
    //
    //
    //             // this.props.navigation.goBack();
    //         })
    //         .catch((error)=> {
    //             alert('错误了'+error);
    //         })
    // }
    componentDidMount(){
        request(
            apiStore.systemConf.banner,
            configure.POST,
            configure.Body('paramKey=banner')
        ).then((result) => {
               let data =result.res.data.paramValue.split(',');
           this.setState({
               images:data

           });

        },(result)=>{
            alert(`error${result}`)
        })
    }

    render() {
        let pics = this.state.images;
        if (pics && pics.length >0) {
            return (
                <Swiper
                    height={200}
                    loop={true}
                    showsButtons={false}
                    showBanner={true}
                    autoplay={true}
                    horizontal={true}
                >
                    {this.renderImages()}
                </Swiper>
            );
        }else {
            return <Image/>
        }

    }

    renderImages() {
        let ivs = [];
        let pc = this.state.images;
        for (let i = 0; i < pc.length;i++) {
            ivs.push(
                <Image
                    key={i}
                    style={{height:200}}
                    source={{uri:`${apiStore.picUrl}${pc[i]}`}}
                />
            );
        }

        return ivs;
    }

}



